//
//  HWMobilePhoneDianhHuaViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/30.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWMobilePhoneDianhHuaViewController.h"
#import "Countdown.h"
#import "HWAssociatedPhoneViewController.h"
#import "HWChangePasswordViewController.h"

@interface HWMobilePhoneDianhHuaViewController ()
@property(strong,nonatomic)UILabel *nameLabel;
@property(strong,nonatomic)UIView *whiteView;
@property(strong,nonatomic)UILabel *describeLabel;
@property(strong,nonatomic)UITextField *titleTextField;
@property(strong,nonatomic)UIView *fenxianView;
@property(strong,nonatomic)UIButton *codeBut;
@property(strong,nonatomic)UIView *lowerPartView;
@property(strong,nonatomic) NSString *dataStr;

@end

@implementation HWMobilePhoneDianhHuaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
   //  self.nameLabel.text = [NSString stringWithFormat:@"绑定手机号：%@",self.phoneStr];
    self.nameLabel.text = [NSString stringWithFormat:@"%@：%@",[HWjianzhiduiObject keylocalization:@"绑定手机号"],self.phoneStr];
}

// 设置UI
- (void)setupUI {
    
     self.dataStr = @"";
    
    self.navigationItem.title = @"手机验证";
    
    self.navigationItem.title = [HWjianzhiduiObject keylocalization:@"手机验证"];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-100)/2, CF_NavHeight+45, 100, 100)];
    imageView.image = [UIImage imageNamed:@"b_qq_phone"];
    [self.view addSubview:imageView];
    
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, imageView.bottom+20, kScreenW, 14)];
    self.nameLabel.font = [UIFont systemFontOfSize:14];
    self.nameLabel.textColor = RGB(51, 51, 51);
    self.nameLabel.text = @"绑定手机号：18330491321";
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
      // self.namelabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.nameLabel];
    
    self.whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, self.nameLabel.bottom+20, kScreenW, 44)];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.whiteView];
    
      self.describeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 55, 44)];
        self.describeLabel.font = [UIFont systemFontOfSize:14];
        self.describeLabel.textColor = RGB(51, 51, 51);
        self.describeLabel.text = @"验证码";
    self.describeLabel.text =  [HWjianzhiduiObject keylocalization: @"验证码"];
    
    
       // self.namelabel.backgroundColor = [UIColor redColor];
    [self.whiteView  addSubview:self.describeLabel];
      
      self.titleTextField = [[UITextField alloc]initWithFrame:CGRectMake( self.describeLabel.right+1, 0, 300, 44)];
    
    
    NSString *qingshu = [HWjianzhiduiObject keylocalization:@"请输入验证码"];
    
      NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:qingshu attributes:
                 @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                   NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                 ];
      self.titleTextField.attributedPlaceholder  = attrString;
      self.titleTextField.font = [UIFont systemFontOfSize:12];
      self.titleTextField.textAlignment = NSTextAlignmentLeft;
      self.titleTextField.textColor = RGB(51, 51, 51);
      [self.whiteView addSubview:self.titleTextField];
      
      
      self.fenxianView = [[UIView alloc]initWithFrame:CGRectMake(0, 43, kScreenW, 1)];
      self.fenxianView.backgroundColor = xiatiaoyanSehui;
     [self.whiteView addSubview:self.fenxianView];
      
      
    NSString *huoqu = [HWjianzhiduiObject keylocalization:@"获取验证码"];
    
         UIButton *xianshyanj = [UIButton buttonWithType:UIButtonTypeCustom];
          xianshyanj.frame = CGRectMake(kScreenW-70-10,4, 70, 35);
          [xianshyanj setTitle:huoqu forState:UIControlStateNormal];
          [xianshyanj setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
         // xianshyanj.titleLabel.textAlignment = NSTextAlignmentRight;
        //  xianshyanj.backgroundColor = [UIColor redColor];
          xianshyanj.titleLabel.font = [UIFont systemFontOfSize:11];
      //    [xianshyanj setBackgroundImage:[UIImage imageNamed:@"sign_surep"] forState:UIControlStateNormal];
      //    [xianshyanj setBackgroundImage:[UIImage imageNamed:@"kaijisign_surep_b"] forState:UIControlStateSelected];
          
     self.codeBut = xianshyanj;
     [self.whiteView  addSubview:xianshyanj];
          
     [xianshyanj addTarget:self action:@selector(xianshyanjTap:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0,self.whiteView.bottom+30, kScreenW, 44)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lowerPartView];
    
    
    NSString *queding  = [HWjianzhiduiObject keylocalization:@"确定"];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(15, 0, kScreenW-15*2, 36) Title:queding Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap)];
    butt.backgroundColor =  [UIColor  bm_colorGradientChangeWithSizeView:butt];
    butt.layer.cornerRadius = 18;
    butt.layer.masksToBounds = YES;
    [self.lowerPartView addSubview:butt];
          
    
    
    
}

// 获取验证码
- (void)xianshyanjTap:(UIButton *)sende {
    
//    if (![WMUtil validateMobile:self.titleTextField.text]) {
//
//        [MBProgressHUD showError:@"请输入正确的手机号"];
//        return ;
//
//
//    }
//

    
    [Countdown  StartTimeCodeBtn:sende];
    
    // 发送验证码
    
    [self shoujiHaoYanzhengma];
    
   
    
    
    
    DLog(@"获取验证码");
    

    
}

//
- (void)buttonTap {
    
      DLog(@"下一步");
    
    // self.titleTextField.text
    
    if (!(self.titleTextField.text.length > 0)) {
        [MBProgressHUD showError:@"验证码不能为空"];
        return;
    }
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    int t = [WMUtil getRandomNumber:101 to:998];
       
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
         NSString *signMD5 = [WMUtil md5HexDigest:sign];
         NSDictionary *parametes = @{
                                      @"ct" :  @"app",
                                      @"ac" : @"remove_mobile",
                                      @"ios" : @"1",
                                      @"uid" : userID,
                                      @"t" : tStr,
                                      @"sign" :signMD5,
                                      @"phone": @"",
                                      @"code" :   self.titleTextField.text,
                                      @"sessionid" : self.dataStr,
           
                                      };
         
         
         
          
          [MBProgressHUD show:@"正在解绑"];
          
          [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
              
            
              NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           //   [MBProgressHUD hideHUD];
              
              
              if ([codeStr intValue] == 1) {
                
            
                 [MBProgressHUD  showSuccess:response[@"message"]];
                  [self.navigationController popViewControllerAnimated:YES];
                  
                  
             
                  
                  
                  

              } else {
               
                   [MBProgressHUD  showSuccess:response[@"message"]];
                  
              }
              NSLog(@"%@",response);
               [MBProgressHUD hideHUD];
              
          } failure:^(NSString *error) {

              [MBProgressHUD  showError:@"修改成功"];

              
          }];
          
         
    
    

    
    
}


- (void)shoujiHaoYanzhengma {
    
    
 
    
    
    
    
    
    
    
    
    
    
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];

    int t =  [WMUtil getRandomNumber:101 to:998];
     NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
     NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"user_codes",
                                @"ios" : @"1",
                                @"uid":userID,
                                @"t" :tStr,
                                @"sign" :signMD5,
                                @"member_id":[HWLocallyStoredData getCode],

                                };
    [MBProgressHUD show:@"正在发送验证码"];
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {


        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];


        if ([codeStr intValue] == 1) {

            //  response[@"data"];

          //  [MBProgressHUD showSuccess:@"验证码发送成功"];

            [MBProgressHUD  showSuccess:response[@"message"]];
            self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];




        } else {
            //  self.dataStr= @"";
             [MBProgressHUD  showError:response[@"message"]];

        }
        NSLog(@"%@",response);


    } failure:^(NSString *error) {

        [MBProgressHUD  showError:@"验证码发送失败"];
       // self.dataStr= @"";

    }];

    
    
    
    
//      NSString *userID = [HWLocallyStoredData getLoginUserID];
//
//     int t =  [WMUtil getRandomNumber:101 to:998];
//      NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
//     NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
//     NSString *signMD5 = [WMUtil md5HexDigest:sign];
//      NSDictionary *parametes = @{
//                                 @"ct" :  @"app",
//                                 @"ac" : @"mobile_code",
//                                 @"ios" : @"1",
//                                 @"type" : @"1",
//                                 @"phone": self.phoneStr
//                                 };
//     [MBProgressHUD show:@"正在发送验证码"];
//     [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//         NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//      //   [MBProgressHUD hideHUD];
//
//
//         if ([codeStr intValue] == 1) {
//
//             //  response[@"data"];
//
//           //  [MBProgressHUD showSuccess:@"验证码发送成功"];
//
//             [MBProgressHUD  showSuccess:response[@"message"]];
//             self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];
//
//
//
//
//         } else {
//             //  self.dataStr= @"";
//              [MBProgressHUD  showError:response[@"message"]];
//
//         }
//         NSLog(@"%@",response);
//
//
//     } failure:^(NSString *error) {
//
//         [MBProgressHUD  showError:@"验证码发送失败"];
//        // self.dataStr= @"";
//
//     }];

    
}







@end
